﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Money.Interfaces;
using Money.Data;
using One.Framework;
using One.Web;
using One.Web.UI.WebControls;
using Resources;

public partial class Admin_EditAccountType : System.Web.UI.Page
{
    #region fields

    private IAccountTypeService accountTypeService;
    private int accountTypeId;
    private AccountType accountType;
    private int success;
    private string redirectUrl = "AccountTypeManagement.aspx";

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        accountTypeService = ServiceFactory.Create<IAccountTypeService>();

        GetParams();
        LoadData();
        AddRules();
        if (!IsPostBack)
        {
            UpdateView();
        }
    }

    protected void AddOrUpdate(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            bool isAdd = false;
            if (accountType == null)
            {
                isAdd = true;
                accountType = new AccountType();
            }
            accountType.Name = tbName.Text;

            if (isAdd)
            {
                accountTypeService.CreateAccountType(accountType);
                success = 1;
            }
            else
            {
                accountTypeService.UpdateAccountType(accountType);
                success = 2;
            }
            Response.Redirect(string.Format("{0}?success={1}", redirectUrl, success));
        }
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        accountTypeId = QueryStringUtility.GetInt32("accountTypeId", 0);
    }

    private void LoadData()
    {
        try
        {
            if (accountTypeId != 0)
            {
                accountType = accountTypeService.GetAccountTypeById(accountTypeId);
            }
        }
        catch
        {
            accountType = null;
        }
    }

    private void UpdateView()
    {
        (Master as Admin_MasterPage).SelectedTab = "accountTypeManagement";
        if (accountType == null)
        {
            pageTitle.Text = Resources.Strings.AddAccountType;
            btAddOrUpdate.Text = Resources.Strings.Add;
        }
        else
        {
            pageTitle.Text = Resources.Strings.UpdateAccountType;
            btAddOrUpdate.Text = Resources.Strings.Update;
            tbName.Text = accountType.Name;
        }
    }



    private void AddRules()
    {
        validationManager.AddRule(new ValueRequiredRule(tbName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));

        validationManager.Notifier = new BalloonNotifier();
    }


    #endregion

    #region public functions


    #endregion
}
